<?php

namespace app\admin\model;

use think\Model;


class ConfigValueModel extends Model
{

    const TYPE_FEEDBACK = 1;//意见反馈
    const TYPE_COMMUNITY = 2;//社区
    const TYPE_STREET = 3;//街道
    const TYPE_FACILITIES = 4;//房屋设施
    const TYPE_HOUSE = 5;//户型
    const TYPE_RENT = 6;//出租类型
    const TYPE_PAYMENT = 7;//付款方式
    const TYPE_TAG = 8;//标签


    // 表名
    protected $name = 'config_value';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    // 追加属性
    protected $append = [
        'group_name',
        //'pid_name',
        'create_time_text',
        'update_time_text'
    ];


    public function parent()
    {
        return $this->belongsTo(ConfigValueModel::class, 'pid', 'id')->joinType('left')->setEagerlyType(0);
    }

    public function getIconAttr($value, $data)
    {
        $value = $value ?: ($data['icon'] ?? '');
        return $value ? cdnurl($value) : '';
    }

    public function getOptionList($cid, $pid = 0)
    {
        $where = [];
        $where['cid'] = $cid;
        if(!is_null($pid)) {
            $where['pid'] = $pid;
        }
        return $this->where($where)
            ->column('option_name', 'option_id');
    }

    public function getOptionListWithIcon($cid, $pid = 0)
    {
        $where = [];
        $where['cid'] = $cid;
        if(!is_null($pid)) {
            $where['pid'] = $pid;
        }
        $list = $this->where($where)
            ->column('option_id,option_name,icon');
        foreach ($list as $k => $v) {
            $list[$k]['icon'] = $v['icon'] ? cdnurl($v['icon']) : '';
        }
        return array_values($list);
    }

    public function getIdByCidOptionId($cid, $option_id)
    {
        return self::where('cid', $cid)
            ->where('option_id', $option_id)
            ->value('id');
    }


    public static function getTagsText($tags)
    {
        if (empty($tags)) {
            return [];
        }
        $tags_arr = (new self())->getOptionList(self::TYPE_TAG);
        $tags = explode(',', $tags);
        $tag_list = [];
        foreach ($tags as $tag) {
            $tag_list[] = $tags_arr[$tag] ?? '';
        }
        return array_filter($tag_list);
    }


    public static function getCommunityList()
    {
        return (new self())->getOptionList(self::TYPE_COMMUNITY);
    }

    public function getGroupNameAttr($value, $data)
    {
        $value = $value ?  : ($data['cid'] ?? '');
        return ConfigGroupModel::where('id', $value)->value('name');
    }

    public function getPidNameAttr($value, $data)
    {
        $value = $value ?  : ($data['pid'] ?? '');
        return $value ? self::where('id', $value)->value('option_name') : '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
